/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Comentario;
import Model.Nota_receita;
import Model.Receita;
import Model.Usuario;
import Util.HibernateUtil;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

/**
 *
 * @author aluno
 */
@WebServlet(name = "ServletComentarios", urlPatterns = {"/ServletComentarios"})
public class ServletComentarios extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String opt = (String) request.getParameter("opt");
        String url = "";
        switch (opt) {
            case "note":
                url = "ServletReceita?opt=detalhe&id=" + insertNote(request);
                break;
            case "comments":
                url = "ServletReceita?opt=detalhe&id=" + insertComments(request);
                break;
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    private String insertNote(HttpServletRequest request) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        
        Integer usuario = Integer.parseInt(request.getParameter("usuario"));
        Integer receita = Integer.parseInt(request.getParameter("receita"));
        Integer nota = Integer.parseInt(request.getParameter("nota"));
       
        Receita rec;
        hSession.beginTransaction();	
        rec = (Receita) hSession.get(Receita.class, receita);
        Nota_receita nota_rec = new Nota_receita();	
        nota_rec.setUsuario_id((Usuario) hSession.get(Usuario.class, usuario));	
        nota_rec.setReceita_id(rec);
        nota_rec.setNota(nota);
        hSession.save(nota_rec);
        hSession.getTransaction().commit();
       
        return String.valueOf(receita);

    }

    private String insertComments(HttpServletRequest request) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        
        Integer usuario = Integer.parseInt(request.getParameter("usuario"));
        Integer receita = Integer.parseInt(request.getParameter("receita"));
        String comentario = request.getParameter("comment");
       
        Receita rec;
        hSession.beginTransaction();	
        rec = (Receita) hSession.get(Receita.class, receita);
        Comentario coment = new Comentario();	
        coment.setUsuario_id((Usuario) hSession.get(Usuario.class, usuario));	
        coment.setReceita_id(rec);
        coment.setComentario(comentario);
        hSession.save(coment);
        hSession.getTransaction().commit();
       
        return String.valueOf(receita);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
